//
//  OYMainController.swift
//  weiboCopycat
//
//  Created by Orange Yu on 2016/11/30.
//  Copyright © 2016年 Orange Yu. All rights reserved.
//

import UIKit
import SVProgressHUD

class OYMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
        let tabBar = OYTabBar()
        tabBar.oyDelegate = self
        setValue(tabBar, forKey: "tabBar")
        
        // 给tabBar的闭包传值
        tabBar.callBack = { [weak self] in
             // MARK: -点击发微博按钮响应事件
            
            if !OYUserInfoViewModel.sharedUserInfo.isLogin {
                SVProgressHUD.showError(withStatus: "请您先登录")
                return
            }
//            let composeView = OYComposeView()
//            let window = UIApplication.shared.windows.last!
//            window.addSubview(composeView)
             // MARK: -将控制器对象传递到composeView以便进行modal
            if let weakSelf = self {
                let composeView = OYComposeView()
                
                composeView.show(target: weakSelf)
            }
          
            
            
        }

        addChildViewController(vc: OYHomeController(), title: "首页", image: "home")
        addChildViewController(vc: OYMessageController(), title: "消息", image: "message_center")
        addChildViewController(vc: OYDiscoverController(), title: "发现", image: "discover")
        addChildViewController(vc: OYProfileController(), title: "我的", image: "profile")
    
    }


    func addChildViewController(vc: UIViewController,title: String,image: String) {
        // 设置标题
        vc.tabBarItem.title = title
        // 设置图片以及图片的渲染模式
        vc.tabBarItem.image = UIImage(named: "tabbar_" + image)?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named:"tabbar_" + image + "_selected" )?.withRenderingMode(.alwaysOriginal)
        // 设置title颜色和字体
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 12)], for: .normal)
        
        vc.navigationItem.title = title
        
        let navVc: OYNavigationController = OYNavigationController(rootViewController: vc)
        
        addChildViewController(navVc)
        
        
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}


extension OYMainController: OYTabBarDelegate {
    func OYTabBarDelegate(oyTabBar: OYTabBar) {

         // MARK: -代理执行的回调
    }
}
